//
//  ExportToViewController.m
//  iExpense
//
//  Created by JackyLiang on 09-7-15.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ExportToViewController.h"


@implementation ExportToViewController
@synthesize addressTextField;
@synthesize invokeAddressBookButton;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	// Set the title of this view
	self.title = @"Export to Email";
	
	// Set the view background to match the grouped tables in the other views.
	self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
	
	// Set the right button Save
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
																							target:self action:@selector(save:)] autorelease];
}

- (IBAction)save:(id)sender {
}

- (IBAction)buttonClicked:(id)sender {
	ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init];
    ppnc.peoplePickerDelegate = self;
    ppnc.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonEmailProperty]];
    [self presentModalViewController:ppnc animated:YES];
    [ppnc release];	
}

// Call this function when the content in the text box is being changed
- (IBAction)addressChanged:(id)sender {
	self.navigationItem.rightBarButtonItem.enabled = (addressTextField.text.length > 0);
}

// Call this function when you hit the Done button in the keyboard
- (IBAction)textFieldDoneEditing:(id)sender {
	[sender resignFirstResponder];
}

// Call this function whenever the view will appear
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	
	[addressTextField becomeFirstResponder];
}

// Call this function whenever the view will disappear
- (void)viewWillDisappear:(BOOL)animated {
    // hides the keyboard
    [addressTextField resignFirstResponder];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
    [self dismissModalViewControllerAnimated:YES];
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
    BOOL done = NO;
    
    if (person) {
        ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
        if (emails && ABMultiValueGetCount(emails) == 1) {
            done = YES;
            NSString *email = (NSString*)ABMultiValueCopyValueAtIndex(emails, 0);
            self.addressTextField.text = email;
            [email release];
        }
        
        if (emails) {
            CFRelease(emails);
        }
    }
    
    if (done) {
        [self dismissModalViewControllerAnimated:YES];
    }
	
    return !done;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
    [self dismissModalViewControllerAnimated:YES];
    
    if (person) {
        ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
        if (emails) {
            CFIndex index = ABMultiValueGetIndexForIdentifier(emails, identifier);
            NSString *email = (NSString*)ABMultiValueCopyValueAtIndex(emails, index);
            self.addressTextField.text = email;
            [email release];
            
            CFRelease(emails);
        }
    }
    return NO;
}


- (void)dealloc {
	[addressTextField release];
	[invokeAddressBookButton release];
    [super dealloc];
}


@end
